import request from '@/api/axios'
import {AxiosPromise} from 'axios'
import {DataRes, DataRecordList} from '@/api/config'
import {OperButton} from '@/components/dTable/dHeader'

export type MenuType = 1 | 2

export interface UserSave {
  role?: string //拥有角色的id
  username: string //姓名
  phone?: string | number //联系电话
  password?:string
  id?: string //id
  job?: string | null //岗位
}

export interface UserEdit extends UserSave {
  id: string
}

//新增登录账号
export const userSave = (
  data: UserSave | UserEdit
): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/user',
    method: 'post',
    data
  })
}
//编辑登录账号
export const useredit = (
  data: UserSave | UserEdit
): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/user',
    method: 'put',
    data
  })
}
//分页查询登录账号数据
export interface AccountParams {
  loginName?: string
  username?: string
  phone?: string
  roleId?: string
  status?: string
  page?: number
  limit?: number
}

export interface AccountList {
  id: string
  userId: string
  status: number
  colorType: string
  button: Array<OperButton>
}

export const getAccountList = (params: AccountParams): AxiosPromise<any> => {
  return request({
    url: '/api/user/list',
    method: 'get',
    params
  })
}

// 删除
export const userRemove = (
  userId): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/user/' + userId,
    method: 'DELETE'
  })
}
// 重置用户密码
export const userResetPwd = (params: {
  userId: string
}): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/user/resetPwd',
    method: 'post',
    params
  })
}
export interface UserResetPwd2 {
  userId: string | number
}
// 修改用户状态
export const changeStatus = (params: {
  userId: string // id
  status: number //账号状态
}): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/user/changeStatus',
    method: 'post',
    params
  })
}

export interface RoleNameList {
  id: string
  roleName: string // 角色名
}

//获取角色下拉列表
export const roleNameList = (): AxiosPromise<DataRes<Array<RoleNameList>>> => {
  return request({
    url: '/api/role/list',
    method: 'get'
  })
}
